package fr.cephb.berkeleydb.db;

import com.sleepycat.bind.tuple.TupleBinding;
import com.sleepycat.je.Database;
import com.sleepycat.je.DatabaseException;
import com.sleepycat.je.Environment;
import com.sleepycat.je.SecondaryConfig;
import com.sleepycat.je.SecondaryDatabase;
import com.sleepycat.je.Transaction;

public class SecondaryDB<K,PRIMARY_KEY,V>
	extends BerkeleyDB<K,V>
	{
	private SecondaryDatabase database;
	private TupleBinding<PRIMARY_KEY> primarykeyBinding;
	public SecondaryDB(
			Environment env,
			Transaction txn,
			String databaseName,
			Database primaryDB,
			SecondaryConfig dbConfig,
			TupleBinding<K> keyBinding,
			TupleBinding<PRIMARY_KEY> primarykeyBinding,
			TupleBinding<V> valueBinding
			) throws DatabaseException
		{
		super(keyBinding,valueBinding);
		dbConfig=(SecondaryConfig)defaultDbCfg(env,dbConfig,true);
		if(databaseName==null)
			{
			databaseName= getDefaultName();
			}
		this.primarykeyBinding=primarykeyBinding;
		assert(this.primarykeyBinding!=null);
		this.database= env.openSecondaryDatabase(
				txn, databaseName, primaryDB,dbConfig
				);
		}
	
	public TupleBinding<PRIMARY_KEY> getPrimaryKeyBinding()
		{
		return primarykeyBinding;
		}
	
	public SecondaryDatabase getSecondaryDatabase()
		{
		return this.database;
		}
	
	@Override
	public Database getDatabase()
		{
		return this.database;
		}
	
	
	
	}
